import numpy as np

def align_corrcoef(trace_base, trace_align):
    slide_distance = min(int(len(trace_base) / 3), 200)
    align_range = (slide_distance, len(trace_base) - slide_distance)
    trace_base_cmp = trace_base[align_range[0]:align_range[1]]
    trace_cmp_point = len(trace_base_cmp)
    corr_list = []
    for i in range(align_range[0] - slide_distance, align_range[0] + slide_distance):
        trace_align_cmp = trace_align[i:i + trace_cmp_point]
        corr_list.append(abs(np.corrcoef(trace_base_cmp, trace_align_cmp)[0, 1]))
    corr_max_index = np.array(corr_list).argmax()
    new_trace_align = np.zeros(len(trace_align))
    shifting = abs(slide_distance - corr_max_index)
    if corr_max_index >= slide_distance:
        new_trace_align[:len(trace_align) - shifting] = trace_align[shifting:]
    else:
        new_trace_align[shifting:] = trace_align[:len(trace_align) - shifting]
    return new_trace_align

def align_norm2(trace_base, trace_align):
    slide_distance=min(int(len(trace_base)/3),200)
    align_range=(slide_distance,len(trace_base) - slide_distance)
    trace_base_cmp = trace_base[align_range[0]:align_range[1]]
    trace_cmp_point = len(trace_base_cmp)
    norm_list = []
    for i in range(align_range[0] - slide_distance, align_range[0] + slide_distance):
        trace_align_cmp = trace_align[i:i + trace_cmp_point]
        norm_list.append(abs(np.linalg.norm(trace_base_cmp - trace_align_cmp)))
    norm_min_index = np.array(norm_list).argmin()
    new_trace_align = np.zeros(len(trace_align))
    shifting = abs(slide_distance - norm_min_index)
    if norm_min_index >= slide_distance:
        new_trace_align[:len(trace_align) - shifting] = trace_align[shifting:]
    else:
        new_trace_align[shifting:] = trace_align[:len(trace_align) - shifting]
    return new_trace_align